*******************************************************************************
* Figure A4. RELATIVE TO 2013 ESTIMATES
********************************************************************************

clear
use "$data/fdiff.dta", clear
set more off

* DEFINE NEW VARIABLES
global x2 migch_rel13
global z2 IV2005_rel13

* CONTROLS
global yyctrl feff sh_pgds sh_wap13 sh_college13 sh_manuf13

* YEARLY ESTIMATES
rename totnet_all_res_13_d totnet_all_13
rename totnet_excl_res_13_d totnet_excl_13
rename totrev_all_res_13_d totrev_all_13
rename totrev_excl_res_13_d totrev_excl_13
rename totexp_all_res_13_d totexp_all_13
rename totexp_excl_res_13_d totexp_excl_13

* NET FISCAL COST

	// All budget
matrix A1 = J(5,3,.)
local i=1
levelsof year if year!=2013, local(levels) 
foreach l of local levels {
qui ivreg2 totnet_all_13 ($x2 = $z2) $yyctrl [aw=$w] if year==`l', robust small
matrix A1[`i',1] = _b[$x2]

qui twostepweakiv 2sls totnet_all_13 ($x2 = $z2) $yyctrl [aw=$w] if year==`l', robust small
estadd local arci = e(ar_cset)	// Anderson-Rubin CI
di e(arci)
gen ci`l' = e(arci) 	// saves it as a varible in data
split ci`l', parse(,)	// split at ,
replace ci`l'1 = subinstr(ci`l'1, "[", "", .) // removes [
replace ci`l'2 = subinstr(ci`l'2, "]", "", .) // removes ]
destring ci`l'1 ci`l'2, replace
sum ci`l'1
return list
matrix A1[`i',2] = r(mean)	// Low-CI
sum ci`l'2
return list
matrix A1[`i',3] = r(mean)	// High-CI
drop ci`l' ci`l'1 ci`l'2

local ++i 
}

	// Restricted budget
matrix A2 = J(5,3,.)
local i=1
levelsof year if year!=2013, local(levels) 
foreach l of local levels {
qui ivreg2 totnet_excl_13 ($x2 = $z2) $yyctrl [aw=$w] if year==`l', robust small
matrix A2[`i',1] = _b[$x2]

qui twostepweakiv 2sls totnet_excl_13 ($x2 = $z2) $yyctrl [aw=$w] if year==`l', robust small
estadd local arci = e(ar_cset)	// Anderson-Rubin CI
di e(arci)
gen ci`l' = e(arci) 	// saves it as a varible in data
split ci`l', parse(,)	// split at ,
replace ci`l'1 = subinstr(ci`l'1, "[", "", .) // removes [
replace ci`l'2 = subinstr(ci`l'2, "]", "", .) // removes ]
destring ci`l'1 ci`l'2, replace
sum ci`l'1
return list
matrix A2[`i',2] = r(mean)	// Low-CI
sum ci`l'2
return list
matrix A2[`i',3] = r(mean)	// High-CI
drop ci`l' ci`l'1 ci`l'2

local ++i 
}

	// Plot
matrix A1=A1'
matrix A2=A2'

coefplot (matrix(A1[1]), ci((A1[2] A1[3])) label(All budget) ///
yline(0, lc(gs4)) ciopts(recast(rcap))) ///
(matrix(A2[1]), ci((A2[2] A2[3])) m(D) msize(medsmall) label(Restricted budget) ///
ciopts(recast(rcap)) graphregion(color(white)) ///
xtitle("Estimated effect", size(small)) xlab(, labsize(small))), vertical ///
coeflabel(1 r1 = "2014" ///
		  2 r2 = "2015" ///
		  3 r3 = "2016" ///
		  4 r4 = "2017" ///
		  5 r5 = "2018" , labsize(small))

* REVENUES

	// All budget
matrix B1 = J(5,3,.)
local i=1
levelsof year if year!=2013, local(levels) 
foreach l of local levels {
qui ivreg2 totrev_all_13 ($x2 = $z2) $yyctrl [aw=$w] if year==`l', robust small
matrix B1[`i',1] = _b[$x2]

qui twostepweakiv 2sls totrev_all_13 ($x2 = $z2) $yyctrl [aw=$w] if year==`l', robust small
estadd local arci = e(ar_cset)	// Anderson-Rubin CI
di e(arci)
gen ci`l' = e(arci) 	// saves it as a varible in data
split ci`l', parse(,)	// split at ,
replace ci`l'1 = subinstr(ci`l'1, "[", "", .) // removes [
replace ci`l'2 = subinstr(ci`l'2, "]", "", .) // removes ]
destring ci`l'1 ci`l'2, replace
sum ci`l'1
return list
matrix B1[`i',2] = r(mean)	// Low-CI
sum ci`l'2
return list
matrix B1[`i',3] = r(mean)	// High-CI
drop ci`l' ci`l'1 ci`l'2

local ++i 
}

	// Restricted budget
matrix B2 = J(5,3,.)
local i=1
levelsof year if year!=2013, local(levels) 
foreach l of local levels {
qui ivreg2 totrev_excl_13 ($x2 = $z2) $yyctrl [aw=$w] if year==`l', robust small
matrix B2[`i',1] = _b[$x2]

qui twostepweakiv 2sls totrev_excl_13 ($x2 = $z2) $yyctrl [aw=$w] if year==`l', robust small
estadd local arci = e(ar_cset)	// Anderson-Rubin CI
di e(arci)
gen ci`l' = e(arci) 	// saves it as a varible in data
split ci`l', parse(,)	// split at ,
replace ci`l'1 = subinstr(ci`l'1, "[", "", .) // removes [
replace ci`l'2 = subinstr(ci`l'2, "]", "", .) // removes ]
destring ci`l'1 ci`l'2, replace
sum ci`l'1
return list
matrix B2[`i',2] = r(mean)	// Low-CI
sum ci`l'2
return list
matrix B2[`i',3] = r(mean)	// High-CI
drop ci`l' ci`l'1 ci`l'2

local ++i 
}

	// Plot
matrix B1=B1'
matrix B2=B2'

coefplot (matrix(B1[1]), ci((B1[2] B1[3])) label(All budget) ///
yline(0, lc(gs4)) ciopts(recast(rcap))) ///
(matrix(B2[1]), ci((B2[2] B2[3])) m(D) msize(medsmall) label(Restricted budget) ///
ciopts(recast(rcap)) graphregion(color(white)) ///
xtitle("Estimated effect", size(small)) xlab(, labsize(small))), vertical ///
coeflabel(1 r1 = "2014" ///
		  2 r2 = "2015" ///
		  3 r3 = "2016" ///
		  4 r4 = "2017" ///
		  5 r5 = "2018" , labsize(small))

* EXPENDITURES

	// All budget
matrix C1 = J(5,3,.)
local i=1
levelsof year if year!=2013, local(levels) 
foreach l of local levels {
qui ivreg2 totexp_all_13 ($x2 = $z2) $yyctrl [aw=$w] if year==`l', robust small
matrix C1[`i',1] = _b[$x2]

qui twostepweakiv 2sls totexp_all_13 ($x2 = $z2) $yyctrl [aw=$w] if year==`l', robust small
estadd local arci = e(ar_cset)	// Anderson-Rubin CI
di e(arci)
gen ci`l' = e(arci) 	// saves it as a varible in data
split ci`l', parse(,)	// split at ,
replace ci`l'1 = subinstr(ci`l'1, "[", "", .) // removes [
replace ci`l'2 = subinstr(ci`l'2, "]", "", .) // removes ]
destring ci`l'1 ci`l'2, replace
sum ci`l'1
return list
matrix C1[`i',2] = r(mean)	// Low-CI
sum ci`l'2
return list
matrix C1[`i',3] = r(mean)	// High-CI
drop ci`l' ci`l'1 ci`l'2

local ++i 
}

	// Restricted budget
matrix C2 = J(5,3,.)
local i=1
levelsof year if year!=2013, local(levels) 
foreach l of local levels {
qui ivreg2 totexp_excl_13 ($x2 = $z2) $yyctrl [aw=$w] if year==`l', robust small
matrix C2[`i',1] = _b[$x2]

qui twostepweakiv 2sls totexp_excl_13 ($x2 = $z2) $yyctrl [aw=$w] if year==`l', robust small
estadd local arci = e(ar_cset)	// Anderson-Rubin CI
di e(arci)
gen ci`l' = e(arci) 	// saves it as a varible in data
split ci`l', parse(,)	// split at ,
replace ci`l'1 = subinstr(ci`l'1, "[", "", .) // removes [
replace ci`l'2 = subinstr(ci`l'2, "]", "", .) // removes ]
destring ci`l'1 ci`l'2, replace
sum ci`l'1
return list
matrix C2[`i',2] = r(mean)	// Low-CI
sum ci`l'2
return list
matrix C2[`i',3] = r(mean)	// High-CI
drop ci`l' ci`l'1 ci`l'2

local ++i 
}

	// Plot
matrix C1=C1'
matrix C2=C2'


coefplot (matrix(C1[1]), ci((C1[2] C1[3])) label(All budget) ///
yline(0, lc(gs4)) ciopts(recast(rcap))) ///
(matrix(C2[1]), ci((C2[2] C2[3])) m(D) msize(medsmall) label(Restricted budget) ///
ciopts(recast(rcap)) graphregion(color(white)) ///
xtitle("Estimated effect", size(small)) xlab(, labsize(small))), vertical ///
coeflabel(1 r1 = "2014" ///
		  2 r2 = "2015" ///
		  3 r3 = "2016" ///
		  4 r4 = "2017" ///
		  5 r5 = "2018" , labsize(small))

